/*
 * @Descripttion: GGB
 * @version: 1.0
 * @Author: GGB
 * @Date: 2021-08-16 20:22:46
 * @LastEditors: GGB
 * @LastEditTime: 2021-08-20 20:24:07
 */
import 'package:flutter/material.dart';
import 'package:flutter_data_center/components/loading.dart';
import 'package:flutter_data_center/controller/camera_controller.dart';
import 'package:get/get.dart';
import './components/video.dart';
import './components/warn_list.dart';

class CameraDetailScreen extends StatefulWidget {
  const CameraDetailScreen({Key key, this.tag}) : super(key: key);
  final String tag;
  static String routeName = "/camera_detail";
  @override
  _CameraDetailScreenState createState() => _CameraDetailScreenState();
}

class _CameraDetailScreenState extends State<CameraDetailScreen> {
  final keyWidget = GlobalKey();
  CameraController _cameraController = Get.put(CameraController());
  double height = 0;
  void calculateSizeAndPosition() =>
      WidgetsBinding.instance.addPostFrameCallback((_) {
        final RenderBox box =
            keyWidget.currentContext.findRenderObject(); // 包含widget的绘制信息
        setState(() {
          height = box.size.height + 90;
        });

        // TODO 获得高度，保存高度  然后通过列表数据， 记录每个大楼层的高度，并存于一个map中 每次滑动都检测
      });
  @override
  void initState() {
    super.initState();
    calculateSizeAndPosition();
    _cameraController.getCameraDetail();
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          elevation: 0,
          backgroundColor: Colors.transparent,
          centerTitle: true,
          title: Text(
            "视频监控",
            style: TextStyle(
              fontSize: 18,
            ),
          ),
        ),
        body: SafeArea(
          child: SingleChildScrollView(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                GetBuilder<CameraController>(
                  builder: (_controller) => Column(
                    key: keyWidget,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Padding(
                        padding: EdgeInsets.symmetric(
                          horizontal: 20,
                        ),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              _controller.cameraDetail.name,
                              style: TextStyle(
                                fontSize: 22,
                              ),
                            ),
                            Container(
                              width: 55,
                              height: 23,
                              alignment: Alignment.center,
                              decoration: BoxDecoration(
                                color: Theme.of(context).primaryColor,
                                borderRadius: BorderRadius.circular(15),
                              ),
                              child: Text(
                                "实时",
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 13,
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                      Hero(
                        tag: widget.tag,
                        child: _controller.cameraDetail?.source?.rtmp != null
                            ? Center(
                                child: Padding(
                                  padding: const EdgeInsets.symmetric(
                                    horizontal: 20,
                                  ),
                                  child: Material(
                                    child: RtspVideo(
                                      url: _controller.cameraDetail.source.rtmp,
                                    ),
                                  ),
                                ),
                              )
                            : Loading(),
                      ),
                    ],
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 25),
                  child: Container(
                    height: MediaQuery.of(context).size.height - height,
                    child: WarnList(),
                  ),
                ),
              ],
            ),
          ),
        ),
      );
}
